<?php
// tao anh voi nen den, vien trang + add text vao anh
function tdxFrameCreate($strFile, $strText){
	$maxWidth = 700;
	$imgInfo = getimagesize($strFile);
	$border = 0;
	if($imgInfo[0] > $maxWidth){
		$percent = floor($maxWidth/$imgInfo[0]*100);
		$new_width = floor($imgInfo[0]/100*$percent) - 8;
		$new_height = floor($imgInfo[1]/100*$percent) - 8;
		$bottomHeight = 100;
		// $imgInfo[0] = $new_width;
		// $imgInfo[1] = $new_height;
	}
	else{
		$new_width = $imgInfo[0];
		$new_height = $imgInfo[1];
		$bottomHeight = 200;
	}
	$padding = floor($maxWidth - $new_width)/2;
	if($padding > 80){
		$xCord = 30;
	}
	else{
		$xCord = $padding;
	}
	#print_r($imgInfo);die;
	// Create a image
	$im = imagecreatetruecolor($maxWidth, $new_height+$bottomHeight);
	$black = imagecolorallocate($im, 0, 0, 0);

	// Set the background to be black
	imagefilledrectangle($im, 0, 0, $maxWidth, $new_height+$bottomHeight, $black);
	// Create a image
	$im2 = imagecreatetruecolor($new_width+4, $new_height+4);
	$white = imagecolorallocate($im2, 255, 255, 255);
	switch($imgInfo['mime']){
		case 'image/jpeg':
		case 'image/jpg':
			$jpg_image = imagecreatefromjpeg($strFile);
		break;
		case 'image/gif':
			$jpg_image = imagecreatefromgif($strFile);
		break;
		case 'image/png':
			$jpg_image = imagecreatefrompng($strFile);
		break;
		default:
			$jpg_image = imagecreatefromjpeg($strFile);
		break;
	}
	if($imgInfo[0] > $maxWidth){
		// echo $new_width.'x'.$new_height;die;
		$image_p = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($image_p, $jpg_image, 0, 0, 0, 0, $new_width, $new_height, $imgInfo[0], $imgInfo[1]);
		$jpg_image = $image_p;
		$border = 2;
	}
	// Set the background to be black
	imagefilledrectangle($im2, 0, 0, $new_width+4, $new_height+4, $white);
	imagecopy($im, $im2, $padding-2, $xCord-2, 0, 0, $new_width+4, $new_height+4);
	$white = imagecolorallocate($im, 255, 255, 255);
	$font_path = 'arial.ttf';

	// First we create our bounding box for the first text
	$bbox = imagettfbbox(25, 0, $font_path, $strText);

	// This is our cordinates for X and Y
	$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2);
	$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2);

	// Print Text On Image
	if(strstr($strText, "\n")){
		$y = $y+100;
	}
	else{
		$y = $y+200;
	}
	// copy anh upload vao nen den
	imagecopy($im, $jpg_image, $padding, $xCord, 0, 0, $new_width, $new_height);
	imagettftext($im, 25, 0, $x, imagesy($im)-50, $white, $font_path, $strText);
	// Send Image to Browser
	return $im;
}